/*
 * Created on 2005/05/24
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;


import java.io.File;


/**
 * 
 *
 */
public abstract class RCPFileApplication extends AbstractBasicFileApplication{

   private String openText="Open File";
   private String saveText="Save File";
   
   private String[] openFilterNames;
   private String[] openFilterExtensions;
   
   private String[] saveFilterNames;
   private String[] saveFilterExtensions;
   
public String[] getOpenFilterExtensions() {
    return openFilterExtensions;
}
public void setOpenFilterExtensions(String[] openFilterExtensions) {
    this.openFilterExtensions = openFilterExtensions;
}
public String[] getOpenFilterNames() {
    return openFilterNames;
}
public void setOpenFilterNames(String[] openFilterNames) {
    this.openFilterNames = openFilterNames;
}
public String[] getSaveFilterExtensions() {
    return saveFilterExtensions;
}
public void setSaveFilterExtensions(String[] saveFilterExtensions) {
    this.saveFilterExtensions = saveFilterExtensions;
}
public String[] getSaveFilterNames() {
    return saveFilterNames;
}
public void setSaveFilterNames(String[] saveFilterNames) {
    this.saveFilterNames = saveFilterNames;
}
   private String openDirectory;
   private String saveDirectory;
   /* (non-Javadoc)
     * @see org.jpn.xucker.commons.util.AbstractBasicFileApplication#execChooseOpenFile()
     */
    protected File execChooseOpenFile() {
        Shell shell=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
        FileDialog dialog=new FileDialog(shell);
        dialog.setText(openText);
        if(openFilterNames!=null){
            dialog.setFilterNames(openFilterNames);
        }
        if(openFilterExtensions!=null){
            dialog.setFilterExtensions(openFilterExtensions);
        }
        if(openDirectory!=null){
            dialog.setFilterPath(openDirectory);
        }
        String path=dialog.open();
        if(path==null){
            return null;
        }
        return new File(path);
    }

    

   

    /* (non-Javadoc)
     * @see org.jpn.xucker.commons.util.AbstractBasicFileApplication#execChooseSaveFile()
     */
    protected File execChooseSaveFile() {
        Shell shell=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
        FileDialog dialog=new FileDialog(shell,SWT.SAVE);
        dialog.setText(saveText);
        if(saveFilterNames!=null){
            dialog.setFilterNames(saveFilterNames);
        }
        if(saveFilterExtensions!=null){
            dialog.setFilterExtensions(saveFilterExtensions);
        }
        if(saveDirectory!=null){
            dialog.setFilterPath(saveDirectory);
        }
        String path=dialog.open();
        if(path==null){
            return null;
        }
        return new File(path);
    }

    /* (non-Javadoc)
     * @see org.jpn.xucker.commons.util.AbstractBasicFileApplication#executeAskSave(java.lang.String, java.lang.String)
     * 
     */
    protected int executeAskSave(String title, String message) {
        Shell shell=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
        MessageBox box = new MessageBox(shell, SWT.YES | SWT.NO | SWT.CANCEL);
        box.setText(title);
        box.setMessage(message);
        int result=box.open();
        if(result==SWT.YES){
            return AbstractBasicFileApplication.YES;
        }else if(result==SWT.NO){
            return AbstractBasicFileApplication.NO;
        }else{
            return AbstractBasicFileApplication.CANCEL;
        }
    }

   

    /* (non-Javadoc)
     * @see org.jpn.xucker.commons.util.AbstractBasicFileApplication#execExit()
     */
    protected void execExit() {
        PlatformUI.getWorkbench().close();
    }
}
